package com.keqing.tomato.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.Locale;

public class TimerService extends Service {
    public static long millisInOneMinute = 1000;
    public static int time_ticking = 0;
    public static int time_finished = 1;
    public static int start_focus = 0;
    public static int start_rest = 1;
    public static int time_btn_state = start_focus;
    private final long countDownInterval = 1000;
    private CountDownTimer countDownTimer;
    private long millisInFuture;
    private String sound;

    public CountDownTimer getCountDownTimer(){
        return countDownTimer;
    }

    public TimerService() {
        Log.e("tomato", "TimerService - TimerService()");
    }

    public static String msToMinSec(long ms) {
        long sec = ms / 1000;
        long min = sec / 60;
        sec -= min * 60;
        return String.format(Locale.getDefault(), "%02d:%02d", min, sec);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("tomato", "TimerService - onCreate()");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("tomato", "TimerService - onStartCommand()");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("tomato", "TimerService - onBind()");
        return new TimerServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("tomato", "TimerService - onUnbind()");
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("tomato", "TimerService - onDestroy()");

    }

    public void addTimer(long millisInFuture, boolean countDown) {
        this.millisInFuture = millisInFuture;
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(millisInFuture + 300, countDownInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if(countDown){
                        String minSec = msToMinSec(millisUntilFinished);
                        Intent intent = new Intent("timer");
                        Bundle bundle = new Bundle();
                        bundle.putString("minSec", minSec);
                        bundle.putInt("what", time_ticking);
                        intent.putExtras(bundle);
                        sendBroadcast(intent);
                    }else{
                        String minSec = msToMinSec(millisInFuture + 300 - millisUntilFinished);
                        Intent intent = new Intent("timer");
                        Bundle bundle = new Bundle();
                        bundle.putString("minSec", minSec);
                        bundle.putInt("what", time_ticking);
                        intent.putExtras(bundle);
                        sendBroadcast(intent);
                    }
                }

                @Override
                public void onFinish() {
                    countDownTimer.cancel();
                    countDownTimer = null;
                    String minSec = msToMinSec(0);
                    Intent intent = new Intent("timer");
                    Bundle bundle = new Bundle();
                    bundle.putString("minSec", minSec);
                    bundle.putInt("what", time_finished);
                    intent.putExtras(bundle);
                    sendBroadcast(intent);
                }
            };
            countDownTimer.start();
        }
    }

    /**************************/
    public class TimerServiceBinder extends Binder {
        public TimerService getTimerService() {
            return TimerService.this;
        }
    }
}
